package org.squarecap.apps.eventcompanion.web;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.squarecap.apps.eventcompanion.domain.AdminCongreso;
import org.squarecap.apps.eventcompanion.domain.Rol;
import org.squarecap.apps.eventcompanion.util.Validators;

@RequestMapping("/admincongresoes")
@Controller
@RooWebScaffold(path = "admincongresoes", formBackingObject = AdminCongreso.class)
public class AdminCongresoController {

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid AdminCongreso adminCongreso, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        Validators.validateUsuario(adminCongreso, bindingResult);
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, adminCongreso);
            return "admincongresoes/create";
        }
        uiModel.asMap().clear();
        adminCongreso.setRol(Rol.findRol(3L));
        adminCongreso.setClave(DigestUtils.sha256Hex(adminCongreso.getClave()));
        adminCongreso.persist();
        System.out.println("ADMINISTRADOR DE CONGRESO PERSISTIDO");
        return "redirect:/admincongresoes/" + encodeUrlPathSegment(adminCongreso.getId().toString(), httpServletRequest);
    }

    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
    public String update(@Valid AdminCongreso adminCongreso, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        Validators.validateUsuario(adminCongreso, bindingResult);
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, adminCongreso);
            return "admincongresoes/update";
        }
        uiModel.asMap().clear();
        adminCongreso.setRol(Rol.findRol(3L));
        adminCongreso.setClave(DigestUtils.sha256Hex(adminCongreso.getClave()));
        adminCongreso.merge();
        return "redirect:/admincongresoes/" + encodeUrlPathSegment(adminCongreso.getId().toString(), httpServletRequest);
    }

    @RequestMapping(value = "/{id}", params = "form", produces = "text/html")
    public String updateForm(@PathVariable("id") Long id, Model uiModel) {
        AdminCongreso admin = AdminCongreso.findAdminCongreso(id);
        admin.setClave("");
        populateEditForm(uiModel, admin);
        return "admincongresoes/update";
    }

    @RequestMapping(params = "form", produces = "text/html")
    public String createForm(Model uiModel) {
        populateEditForm(uiModel, new AdminCongreso());
        return "admincongresoes/create";
    }
}
